import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien
pygame.init()
class AlienInvasion :
    """control sources and behavior"""
    def __init__(self):
        """Initialization"""
        #pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")
        #self.bg_color = (230, 230, 230)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()#use sprite
        self.aliens = pygame.sprite.Group()
        self._create_fleet()
    def run_game(self):
        """start main circulation"""
        while True :

            self._check_events()
            self.ship.update()
            self.bullets.update()

            for bullet in self.bullets.copy():
                if bullet.rect.bottom <= 0:
                    self.bullets.remove(bullet)
            #print(len(self.bullets))
            self._update_screen()

    def _check_events(self):
        """check keyboard and mouse"""
        for event in pygame.event.get() :

            if  event.type == pygame.QUIT :
                   sys.exit()  # 控制退出
            """move the ship"""
            if event.type == pygame.KEYDOWN :
                     #move to right
                 self._check_key_down_events(event)

            if event.type == pygame.KEYUP :
                 self._check_key_up_events(event)
    def _check_key_down_events(self,event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
              pygame.quit()
              sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()



    def _check_key_up_events(self,event):
        if event.key == pygame.K_RIGHT:
            # move to right
            self.ship.moving_right = False

        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False
    def _fire_bullet(self):
        """create a bullet and take into bullets"""
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)
    def _create_fleet(self):
        alien = Alien(self)
        # self.aliens.add(alien)
        alien_width,alien_height = alien.rect.size
        avaliable_space_x = self.settings.screen_width -(2 * alien_width)
        number_aliens_x = avaliable_space_x // (2* alien_width)
        ship_height = self.ship.rect.height
        avaliable_space_y = self.settings.screen_height -(3*alien_height)-ship_height
        number_aliens_y = avaliable_space_y // (2*alien_height)
        for row_number in range(number_aliens_y) :
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number,row_number)

    def _create_alien(self,alien_number,row_number):
        alien = Alien(self)
        alien_width,alien_height = alien.rect.size
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2 * alien.rect.height * (row_number-2)
        self.aliens.add(alien)

    def _update_screen(self):
        """change windows"""
        # every starting for color
        self.screen.fill(self.settings.bg_color)
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)
        pygame.display.flip()
if __name__ == '__main__' :
    ai = AlienInvasion()#1
    ai.run_game()
